package com.sdp.refund.db.refund_area.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.refund.db.refund_area.entity.RefundArea;
import com.sdp.refund.db.refund_area.mapper.RefundAreaMapper;
import com.sdp.refund.db.refund_area.service.IRefundAreaService;
import com.sdp.refund.utils.RefundConstants;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2024-08-27
 */
@Service
public class RefundAreaServiceImpl extends ServiceImpl<RefundAreaMapper, RefundArea> implements IRefundAreaService {

	@Override
	public List<JSONObject> queryAreaTableTree() {
		LambdaQueryWrapper<RefundArea> queryWrapper = new LambdaQueryWrapper<>();
		List<RefundArea> areaList = list(queryWrapper);
		List<RefundArea> rootList = new ArrayList<RefundArea>();
		if(areaList!=null && areaList.size()>0) {
			for(RefundArea area : areaList) {
				if(StringUtils.isEmpty(area.getParentArea()) || RefundConstants.否.getCode().equals(area.getParentArea())) {
					rootList.add(area);
				}
			}
		}
		List<JSONObject> treeList = new ArrayList<JSONObject>();
		JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
		if(rootList!=null && rootList.size()>0) {
			for(int i=0;i<rootList.size();i++) {
				JSONObject parentObj = JSON.parseObject(JSON.toJSONStringWithDateFormat(rootList.get(i), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue,SerializerFeature.WriteDateUseDateFormat));
				treeList.add(parentObj);
				getChildAreaTableTree(parentObj, areaList);
			}
		}
		return treeList;
	}
	
	@SuppressWarnings("unchecked")
	private void getChildAreaTableTree(JSONObject parentObj,List<RefundArea> areaList) {
		List<Object> childList = new ArrayList<Object>();
		for(int j=0;j<areaList.size();j++) {
			if(!StringUtils.isEmpty(areaList.get(j).getParentArea()) && parentObj.getString("areaCode").equals(areaList.get(j).getParentArea())) {
				
				if(parentObj.get("children")==null) {
					parentObj.put("children", childList);
				}else {
					childList = (List<Object>) parentObj.get("children");
				}
				JSONObject childObj = JSON.parseObject(JSON.toJSONStringWithDateFormat(areaList.get(j),"yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteMapNullValue,SerializerFeature.WriteDateUseDateFormat).toString());
				childList.add(childObj);
				getChildAreaTableTree(childObj, areaList);
			}
			
		}
	}

	@Override
	public RefundArea queryAreaByCode(String areaCode) {
		LambdaQueryWrapper<RefundArea> query = new LambdaQueryWrapper<>();
		query.eq(RefundArea::getAreaCode, areaCode);
		return getOne(query,false);
	}

	@Override
	public List<RefundArea> queryAreaByType(String areaType) {
		LambdaQueryWrapper<RefundArea> query = new LambdaQueryWrapper<>();
		query.eq(RefundArea::getAreaType, areaType);
		return list(query);
	}

	@Override
	public List<JSONObject> queryAreaTreeSelect() {
		List<JSONObject> treeList = new ArrayList<JSONObject>();
		List<RefundArea> areaList = list();
		List<RefundArea> rootList = new ArrayList<RefundArea>();
		List<String> rootKeys = new ArrayList<String>();
		if(areaList!=null && areaList.size()>0) {
			for(RefundArea area : areaList) {
				if(StringUtils.isEmpty(area.getParentArea()) || RefundConstants.否.getCode().equals(area.getParentArea())) {
					rootList.add(area);
					rootKeys.add(area.getKeyId());
				}
			}
		}
		
		if(rootList!=null && rootList.size()>0) {
			
			for(int i=0;i<rootList.size();i++) {
				JSONObject parentObj = new JSONObject();
				parentObj.put("title", rootList.get(i).getAreaName());
				parentObj.put("value", rootList.get(i).getAreaCode());
				parentObj.put("key",rootList.get(i).getKeyId());
				parentObj.put("nodeData", rootList.get(i));
				treeList.add(parentObj);
				getChildAreaTreeSelect(parentObj, areaList);
			}
		}
		
		return treeList;
	}
	
	
	
	@SuppressWarnings({ "unchecked" })
	private void getChildAreaTreeSelect(JSONObject parentObj,List<RefundArea> areaList) {
		List<Object> childList = new ArrayList<Object>();
	
		for(int j=0;j<areaList.size();j++) {
			if(!StringUtils.isEmpty(areaList.get(j).getParentArea()) && parentObj.getString("value").equals(areaList.get(j).getParentArea())) {
				
				if(parentObj.get("children")==null) {
					parentObj.put("children", childList);
				}else {
					childList = (List<Object>) parentObj.get("children");
				}
				
				JSONObject childObj = new JSONObject();
				childObj.put("title", areaList.get(j).getAreaName());
				childObj.put("value", areaList.get(j).getAreaCode());
				childObj.put("key",areaList.get(j).getKeyId());
				childObj.put("nodeData",areaList.get(j));
				childList.add(childObj);
				getChildAreaTreeSelect(childObj, areaList);
			}
			
		}
	}

}
